package com.qlmjx.sevalopos;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;


import com.qlmjx.sevalopos.utils.ConstantsUtils;
import com.universal.frame.generalutils.ApkUtil;
import com.universal.frame.generalutils.Constants;
import com.universal.frame.generalutils.GeneralUtil;
import com.universal.frame.generalutils.SharedPreferencesUtil;

/**
 * @author RoreyDiu
 * @TitleName 启动页
 */
public class SplashActivity extends Activity {

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    toWelcomOrMainActivity();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneralUtil.getWindowAttrribute(this);
//        setSwipeBackEnable(false);
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                mHandler.sendEmptyMessage(0);
            }
        }, 1000);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    /**
     * 是否启动引导页
     */
    private void toWelcomOrMainActivity() {
        boolean isFirstRun = true;
        String versionName = ApkUtil.getVersionName(this);
        if (null != SharedPreferencesUtil.getString(this, Constants.LAUNCHE_SPLASH_FLAG)) {
            isFirstRun = !SharedPreferencesUtil.getString(this, Constants.LAUNCHE_SPLASH_FLAG)
                    .equals(versionName);
        }
        if (isFirstRun) {
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
        } else {
            if(SharedPreferencesUtil.getBoolean(this, ConstantsUtils.IS_LOGIN,false)){
                startActivity(new Intent(SplashActivity.this, MainActivity.class));
            }else{
                startActivity(new Intent(SplashActivity.this, LoginActivity.class));
            }
        }
        SharedPreferencesUtil.putString(this, Constants.LAUNCHE_SPLASH_FLAG, versionName);
        SplashActivity.this.finish();
    }


}
